#!/usr/bin/python

# Imports
import os
import opencv
from opencv.cv import *
from opencv.highgui import *

########## Variables ##########

# Database configuration
db_host = "localhost"
db_user =  "root"
db_passwd = ""

# Amount of seconds untill the login sequence
countdown_seconds = 5

# Amount of pictures taken when logging in
pictures_taken = 1

# Maximum amount of pictures to me saved in the database
max_amount_of_pics = 20

# Color of the ellipses around the faces in the pictures and fingerprint.
ellipse_color = 0, 255, 0 # Green

# Amount of possible matches showed after the login sequence.
amount_of_matches = 5

########## Methods ##########

def create_or_update_user_faces(FD, sql, base = 'capture/'):
    """If the fingerprint.png has been changed this method is used to update or create users_faces's the diffs in the database to the new and correct values.
    Only works with the taken pictures, because the pic_names have to correspond to the pic_names in the database.
    Parameters: FaceDetect FD - instance of FaceDetect()
                SQL sql - instance of SQL()
                string base - basepath"""
    faces = os.listdir(base)
    faces.sort()
    
    for pic in faces:
        filename = os.path.join(base, pic)
        shape_diff, temp_diff = FD.match(filename)
        user_id, rest = pic.split('_')
        if sql.user_face_exists(pic):
            face_id = sql.get_user_face(pic)
            sql.update_user_face(shape_diff, temp_diff, pic, face_id)
            print 'Updated user_face for', pic
        else:
            sql.create_user_face(shape_diff, temp_diff, pic, user_id)
            print 'Created user_face for', pic
        
def set_background_colors(base, pictures):
    """Allows you to change the background color of one or multiple pictures.
    Color can be set above.
    Parameters: string base - basepath
                list pictures - list of image names"""
    if len(pictures[0]) == 1:
        filename = os.path.join(base, pictures)
        img = cvLoadImage(filename)
        fill_background(filename, img)
    else:
        for picture in pictures:
            filename = os.path.join(base, picture)
            img = cvLoadImage(filename)
            fill_background(filename, img)

def fill_background(filename, img):
    """Fills the background of a picture with ellipses.
    Parameters: string filename - location of the image
                IplImage img - image"""
    width = img.width / 2
    height = img.height / 2
    while width < (img.width / 2 + 25) and height < (img.height / 2 + 25):
        cvEllipse(img, cvPoint(img.width / 2, img.height / 2), cvSize(width, height), 0, 0, 360, ellipse_color, 2, 8, 0)
        width += 1
        height += 1
    cvSaveImage(filename, img)
    print 'Resaved:', filename

def create_test_users(sql):
    """Creates 50 test_users
    Parameters: SQL sql - instance of SQL()"""
    name = 1
    max_name = 50
    while name <= max_name:
        if not sql.user_exists(login_name = 'User_%s' % name):
            sql.create_user('User %s' % str(name))
        name += 1
